package com.ecube.maintenance.components.fragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ecube.maintenance.R;
import com.ecube.maintenance.biz.apis.impl.UserAction;
import com.ecube.maintenance.biz.commons.ICCallBack;
import com.ecube.maintenance.biz.commons.ICTException;
import com.ecube.maintenance.components.widget.ChoosePictureDialog;
import com.ecube.maintenance.components.widget.LoadingDialog;
import com.ecube.maintenance.components.widget.UserInfoView;
import com.ecube.maintenance.pojos.UserInfo;
import com.ecube.maintenance.utils.Bog;
import com.ecube.maintenance.utils.ImageUtil;
import com.ecube.maintenance.utils.StringUtil;
import com.makeramen.RoundedImageView;

import java.io.File;

public class UserInfoFragment extends Fragment implements ChoosePictureDialog.OnPickListener {
    UserInfo mUserInfo;
    UserInfoView uiv;

    RoundedImageView userHead;
    ChoosePictureDialog choosePictureDialog;

    public UserInfoFragment() {
    }

    public static UserInfoFragment newInstance() {
        UserInfoFragment fragment = new UserInfoFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.clear();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
    @Override
    public void onResume() {
        super.onResume();
        configureActionBar();

    }

    private void configureActionBar() {
        ActionBar actionBar = getActivity().getActionBar();
        assert actionBar != null;
        View v = actionBar.getCustomView();
        if (v != null) {
            v.setVisibility(View.GONE);
        }
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("车辆资料");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
        uiv = (UserInfoView) rootView.findViewById(R.id.view_user_info);
        rootView.findViewById(R.id.btn_save_user_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveUserInfoToServer();

            }
        });
        userHead = (RoundedImageView) rootView.findViewById(R.id.user_head);
        userHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choosePictureDialog = new ChoosePictureDialog(UserInfoFragment.this, "选择方式");
                choosePictureDialog.setOnPickListener(UserInfoFragment.this);
                choosePictureDialog.show();
            }
        });
        refreshUserHead();
        return rootView;
    }

    void refreshUserHead() {
        String url = UserInfo.getInstance().getLogo();
        if (url == null) return;
        Bitmap bm = ImageUtil.getScaleOptionImageByScaleOfWinWidth(
                getActivity(), new File(url), 0.5f
        );
        if (bm != null) {
            userHead.setImageBitmap(bm);
        }
    }

    void saveUserInfoToServer() {
        if (!uiv.checkUserInput())return;

        mUserInfo = uiv.getLatestUserInfo();
        final LoadingDialog dialog = new LoadingDialog(getActivity());
        dialog.setTipText(getString(R.string.message_loading_user_info_update));
        dialog.show();
        UserAction.updates(mUserInfo, new ICCallBack() {
            @Override
            public void done(Object result, ICTException exception) {
                dialog.getDialog().dismissDialogger();
                if (exception == null) {
                    UserInfo.saveToSpf(mUserInfo);
                    Bog.toast("保存成功");
                    getFragmentManager().popBackStack();
                } else {
                    Bog.toast( exception.getMessage());
                }
            }
        });

    }

    @Override
    public void onPickedImage(Bitmap bm, File fullSource) {
        UserInfo.getInstance().setLogo(fullSource.getPath());
        UserInfo.saveToSpf(UserInfo.getInstance());
        userHead.setImageBitmap(bm);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        choosePictureDialog.onActivityResult(requestCode, resultCode, data);
    }
}
